﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.Utils;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraTreeList.Nodes.Operations;
using DevExpress.XtraTreeList.Nodes;

namespace CommonControl
{
    public class DxGridHelper
    {
        /// <summary>
        /// 数据网格数字列
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void DrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;
            if (e.Info.IsRowIndicator)
            {
                if (e.RowHandle >= 0)
                {
                    e.Info.DisplayText = e.RowHandle.ToString();
                }
                else
                {
                    if (e.RowHandle < 0 && e.RowHandle > -1000)
                    {
                        e.Info.Appearance.BackColor = System.Drawing.Color.AntiqueWhite;
                        e.Info.DisplayText = e.RowHandle.ToString();
                    }
                }
            }
        }

        /// <summary>
        /// 数据树网格数字列
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void DrawRowIndicator(object sender, DevExpress.XtraTreeList.CustomDrawNodeIndicatorEventArgs e)
        {
            var tmpTree = sender as DevExpress.XtraTreeList.TreeList;
            e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;
            var args = e.ObjectArgs as DevExpress.Utils.Drawing.IndicatorObjectInfoArgs;
            if (args != null)
            {
                var rowNum = tmpTree.GetVisibleIndexByNode(e.Node) + 1;
                args.DisplayText = rowNum.ToString();
            }
        }


        /// <summary>
        /// 初始网格样式
        /// </summary>
        /// <param name="gdvUser"></param>
        public static void InitGridStyle(GridView view)
        {
            var app = view.Appearance;
            app.OddRow.BackColor = DxGridStyle.Color_odd;
            app.OddRow.Options.UseBackColor = true;
            app.EvenRow.BackColor = DxGridStyle.Color_even;
            app.EvenRow.Options.UseBackColor = true;
            app.FocusedRow.BackColor = DxGridStyle.Color_focused;
            app.FocusedRow.Options.UseBackColor = true;
            app.FocusedCell.BackColor = DxGridStyle.Color_focused_cell;
            app.FocusedCell.Options.UseBackColor = true;
            view.OptionsView.EnableAppearanceEvenRow = true;
            view.OptionsView.EnableAppearanceOddRow = true;
            view.RowHeight = 30;
        }

        /// <summary>
        /// 初始树网格样式
        /// </summary>
        /// <param name="treelist"></param>
        public static void InitGridStyle(DevExpress.XtraTreeList.TreeList treelist)
        {
            var app = treelist.Appearance;
            app.OddRow.BackColor = DxGridStyle.Color_odd;
            app.OddRow.Options.UseBackColor = true;
            app.EvenRow.BackColor = DxGridStyle.Color_even;
            app.EvenRow.Options.UseBackColor = true;
            app.FocusedRow.BackColor = DxGridStyle.Color_focused;
            app.FocusedRow.Options.UseBackColor = true;
            app.FocusedCell.BackColor = DxGridStyle.Color_focused_cell;
            app.FocusedCell.Options.UseBackColor = true;
            treelist.OptionsView.EnableAppearanceEvenRow = true;
            treelist.OptionsView.EnableAppearanceOddRow = true;
            treelist.RowHeight = 30;
        }

        //树网格双击展开收缩 
        public class CollapseExceptSpecifiedOperation : TreeListOperation
        {
            TreeListNode visibleNode;
            public CollapseExceptSpecifiedOperation(TreeListNode visibleNode)
                : base()
            {
                this.visibleNode = visibleNode;
            }
            public override void Execute(TreeListNode node)
            {
                if (!visibleNode.HasAsParent(node))
                    node.Expanded = false;
            }
            public override bool NeedsFullIteration { get { return false; } }
        }
    }
}
